import Cookies from 'js-cookie'
import { getOneQueryByKey, removeQuery } from './url'

const TokenKey = 'Admin-Token'

export function getToken() {
  let queryToken = getOneQueryByKey('token')
  // alert(queryToken)
  if (queryToken) {
    if (Cookies.get(TokenKey) !== queryToken) {
      setToken(queryToken)
    }
    removeQuery('token')
  }
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  // 兼容代码
  localStorage.removeItem('X-Token')
  localStorage.removeItem('X-Token-Web')
  // 兼容代码
  return Cookies.remove(TokenKey)
}
